package svc

import (
	"github.com/zeromicro/go-queue/kq"
	"navi-mumbai/app/rpc/model/pg/usercenter"
	"navi-mumbai/app/rpc/model/pg/wallet"
	"navi-mumbai/app/rpc/wallet/internal/config"
	"navi-mumbai/third/appsflyer"
	"time"

	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

type ServiceContext struct {
	Config config.Config

	UserWalletModel               wallet.UserWalletModel
	WalletMoneyConfigModel        wallet.WalletMoneyConfigModel
	CurrencyExchangeConfigModel   wallet.CurrencyExchangeConfigModel
	PayChannelConfigModel         wallet.PayChannelConfigModel
	UserDepositFlowModel          wallet.UserDepositFlowModel
	UserWithdrawFlowModel         wallet.UserWithdrawFlowModel
	UserGameOrderFlowModel        wallet.UserGameOrderFlowModel
	UserTransferToGameFlowModel   wallet.UserTransferToGameFlowModel
	UserTransferFromGameFlowModel wallet.UserTransferFromGameFlowModel
	UserGameWalletModel           wallet.UserGameWalletModel
	UserInviteOrderFlowModel      usercenter.UserInviteOrderFlowModel
	UserInviteModel               usercenter.UserInviteModel
	UserInviteConfigModel         usercenter.UserInviteConfigModel
	UserAccountModel              usercenter.UserAccountModel
	UserNotifyMsgModel            usercenter.UserNotifyMsgModel
	UserNotifyMsgStatModel        usercenter.UserNotifyMsgStatModel
	KqAddUserNotifyMsgClient      *kq.Pusher
}

func NewServiceContext(c config.Config) *ServiceContext {
	sqlConn := sqlx.NewMysql(c.DB.DataSource)
	sqlUserReadConn := sqlx.NewMysql(c.UserReadDB.DataSource)
	//sqlConn := sqlx.NewSqlConn(dbkey.PostgresDriverName, c.DB.DataSource)
	appsflyer.InitHttpServer()
	return &ServiceContext{
		Config:                        c,
		UserWalletModel:               wallet.NewUserWalletModel(sqlConn, c.Cache),
		WalletMoneyConfigModel:        wallet.NewWalletMoneyConfigModel(sqlConn, c.Cache),
		CurrencyExchangeConfigModel:   wallet.NewCurrencyExchangeConfigModel(sqlConn, c.Cache),
		PayChannelConfigModel:         wallet.NewPayChannelConfigModel(sqlConn, c.Cache),
		UserGameWalletModel:           wallet.NewUserGameWalletModel(sqlConn, c.Cache),
		UserDepositFlowModel:          wallet.NewUserDepositFlowModel(sqlConn, c.Cache),
		UserWithdrawFlowModel:         wallet.NewUserWithdrawFlowModel(sqlConn, c.Cache),
		UserGameOrderFlowModel:        wallet.NewUserGameOrderFlowModel(sqlConn, c.Cache),
		UserTransferToGameFlowModel:   wallet.NewUserTransferToGameFlowModel(sqlConn, c.Cache),
		UserTransferFromGameFlowModel: wallet.NewUserTransferFromGameFlowModel(sqlConn, c.Cache),
		UserInviteOrderFlowModel:      usercenter.NewUserInviteOrderFlowModel(sqlUserReadConn, c.Cache),
		UserInviteModel:               usercenter.NewUserInviteModel(sqlUserReadConn, c.Cache),
		UserInviteConfigModel:         usercenter.NewUserInviteConfigModel(sqlUserReadConn, c.Cache),
		UserAccountModel:              usercenter.NewUserAccountModel(sqlUserReadConn, c.Cache),
		UserNotifyMsgModel:            usercenter.NewUserNotifyMsgModel(sqlUserReadConn, c.Cache),
		UserNotifyMsgStatModel:        usercenter.NewUserNotifyMsgStatModel(sqlUserReadConn, c.Cache),
		//KqAddUserNotifyMsgClient:      kq.NewPusher(c.KqAddUserNotifyMsgClientConf.Brokers, c.KqAddUserNotifyMsgClientConf.Topic),
	}
}

func cOption(o *cache.Options) {
	o.NotFoundExpiry = 1 * time.Minute
	o.Expiry = 60 * time.Minute
}
